package com.groupsix.controller;

import com.groupsix.service.StudentService;
import com.groupsix.utils.PageInfo;
import com.groupsix.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 董盛昌
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 学生控制器
 * @date 2021/10/17 10:47
 */
@Controller
public class StudentController {

    // 依赖注入
    @Autowired
    private StudentService studentService;
    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping("/findStudent.action")
    public String findStudent(String sName, Integer sStudentid,Integer sClassid, String sClassname,
                              Integer pageIndex, Integer pageSize, Model model) {

        PageInfo pi = studentService.findPageInfo(sName,sStudentid,sClassid,
                sClassname,pageIndex,pageSize);
        model.addAttribute("pi",pi);
        model.addAttribute("sName",sName);
        return "student_list";
    }


    /**
     * 删除学生信息
     */
    @RequestMapping( "/deleteStudent.action")
    @ResponseBody
    public String deleteStudent(Integer sId) {
        int s = studentService.deleteStudent(sId);
        return "student_list";
    }

    /**
     * 添加学生信息
     */

    @RequestMapping("/addStudent.action")
    @ResponseBody
    public String addStudent(@RequestBody Student student) {
        int s = studentService.addStudent(student);
        return "student_list";
    }

    /**
     * 修改学生信息
     */
    @RequestMapping("/updateStudent.action")
    public String updateStudent( Student student) {
        int s = studentService.updateStudent(student);
        return "redirect:/findStudent.action";
    }


    @RequestMapping( "/findStudentById.action")
    public String findStudentById(Integer sId, HttpSession session) {

        Student s= studentService.findStudentById(sId);
        session.setAttribute("s",s);
        return "student_edit";
    }
}
